package vn.com.demand.widget;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import vn.com.demand.lux.Activator;
import vn.com.demand.util.DpUtils;

import com.swtdesigner.ResourceManager;

public class SearchableDialogActivator extends Composite {

	private Text text;
	private SelectItemCodeHeader dialog;
	private FilterSQLProvider provider;
	private ItemSelectionCallback callback;
	/**
	 * Create the composite
	 * @param parent
	 * @param style
	 */
	public SearchableDialogActivator(Composite parent, int style, final SelectItemCodeHeader dialog) {
		super(parent, style);
		setLayout(new FormLayout());
		this.dialog = dialog;

		text = new Text(this, SWT.BORDER);
		final FormData fd_text = new FormData();
		fd_text.bottom = new FormAttachment(0, 20);
		fd_text.top = new FormAttachment(0, 2);
		fd_text.left = new FormAttachment(0, 0);
		text.setLayoutData(fd_text);

		Button button;
		button = new Button(this, SWT.NONE);
		fd_text.right = new FormAttachment(button, 0, SWT.LEFT);
		final FormData fd_button = new FormData();
		fd_button.left = new FormAttachment(100, -25);
		fd_button.bottom = new FormAttachment(0, 20);
		fd_button.top = new FormAttachment(0, 2);
		fd_button.right = new FormAttachment(100, 0);
		button.setLayoutData(fd_button);
		button.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/viewmag+.png"));
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {				
				doOpen();
			}
			
		});
		//
	}

	public void setFilterSQLProvider(FilterSQLProvider provider) {
		this.provider = provider;
	}
	
	public void setItemSelectedHandler(ItemSelectionCallback caller) {
		this.callback = caller;
	}
	
	private void doOpen() {
		try {
			if (null != provider) {
				dialog.setItemBrowserCommand(provider.getBrowserCommand());
			}
			if (dialog.open() == Dialog.OK) {
				String[] items = dialog.getSelectedItems();
				StringBuffer b = new StringBuffer(128);
				for (int i = 0; i < items.length; i++) {
					b.append(DpUtils.trim(items[i])).append(",");
				}
				String s = b.toString();
				text.setText(s.substring(0, s.length() - 1));
				text.setFocus();
				text.setBackground(new Color(null, 255, 255, 255));
				if (null != callback) {
					callback.itemSelectedHandler(dialog.getSelectedItem());
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			MessageDialog.openError(null, "Error", e.getMessage());
		}
	}
	
	@Override
	public void dispose() {
		super.dispose();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public String getText() {
		return DpUtils.trim(text.getText());
	}
	
	public void setModifyListner(ModifyListener listener) {
		text.addModifyListener(listener);
	}
	
	public void setFocusListener(FocusListener listener) {
		text.addFocusListener(listener);
	}

	public void setText(String costCenter) {
		text.setText(costCenter);
	}

	@Override
	public void setBackground(Color color) {
		text.setBackground(color);
	}

	@Override
	public boolean setFocus() {
		return text.setFocus();
	}
	
	public void setShellTitle(String title) {
		dialog.getShell().setText(title);
	}
	
	public void setMessageTitle(String title) {
		dialog.setTitle(title);
	}
	
	public void setMessage(String message) {
		dialog.setMessage(message);
	}
	
	
}